package com.xzy.rabbit.demo.deadqueue.a;

import com.rabbitmq.client.Connection;
import com.xzy.rabbit.common.util.RabbitUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 死信队列_消息超时
 *
 * @author xzy
 * @date 2021/9/2811:19
 */
class Main {
    /**
     * 发送的消息总数
     */
    private static final Integer MESSAGE_COUNT = 100;
    /**
     * 发送每条消息的延迟时间
     */
    private static final long PRODUCE_WORK_TIME = 1000L;
    /**
     * 消费每条消息的延迟时间
     */
    private static final long CONSUME_WORK_TIME = 3000L;
    /**
     * 每条消息的过期时间
     */
    private static final long TTL = 1000L;

    private static final String WORK_EXCHANGE_NAME = "xzy.exchange.dead-ttl.work";
    private static final String WORK_ROUTING_KEY = "work";
    private static final String WORK_QUEUE_NAME = "xzy.queue.dead-ttl.work";

    private static final String DEAD_EXCHANGE_NAME = "xzy.exchange.dead-ttl.dead";
    private static final String DEAD_ROUTING_KEY = "dead";
    private static final String DEAD_QUEUE_NAME = "xzy.queue.dead-ttl.dead";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 1.获取连接
        Connection connection = RabbitUtil.getConnection();

        // 2.发送消息
        Thread producer = new Thread(new Producer(connection, WORK_EXCHANGE_NAME, WORK_ROUTING_KEY, MESSAGE_COUNT, PRODUCE_WORK_TIME, TTL), "PRODUCER");

        // 3.接收消息
        Thread workConsumer = new Thread(new ConsumerNormal(connection, WORK_EXCHANGE_NAME, WORK_ROUTING_KEY, WORK_QUEUE_NAME, CONSUME_WORK_TIME, DEAD_EXCHANGE_NAME, DEAD_ROUTING_KEY), "CONSUMER_WORK");
        Thread deadConsumer = new Thread(new ConsumerDead(connection, DEAD_EXCHANGE_NAME, DEAD_ROUTING_KEY, DEAD_QUEUE_NAME), "CONSUMER_DEAD");

        producer.start();
        workConsumer.start();
        deadConsumer.start();

        producer.join();
        workConsumer.join();
        deadConsumer.join();

        /*
         * 死信形成条件：
         *     1.消息过期
         *     2.消息被拒
         *     3.队列已满
         */
    }
}
